package com.bot.adms.dao.impl;

import com.bot.adms.dao.IAdSpScDetailPosterDao;
import com.bot.adms.model.entity.AdSpScDetailPoster;
import com.bot.adms.utils.JdbcUtils;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 海报广告持久层接口
 *
 * @author 石景坤
 * @version 1.0
 */
public class AdSpScDetailPosterDaoImpl implements IAdSpScDetailPosterDao {
    /**
     * 插入海报信息
     *
     * @param adSpScDetailPoster 海报广告信息，包含（标识，海报地址，分辨率，广告分屏明细id）
     * @return 成功条数
     */
    @Override
    public Integer insertAdSpScDetailPoster(AdSpScDetailPoster adSpScDetailPoster) {

        Connection conn = null;
        Statement statement = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "INSERT INTO `ad_sp_sc_detail_poster` (`poster_url`,`resolution`,`ad_sp_sc_detail_id`) VALUES (" +
                    "'" + adSpScDetailPoster.getPosterUrl() + "'," +
                    "" + adSpScDetailPoster.getResolution() + "," +
                    "" + adSpScDetailPoster.getAdSpScDetailId() + ")";

            //4.执行sql
            int count = statement.executeUpdate(sql);

            //5.返回成功条数
            return count;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement);
        }

    }

    /**
     * 更新海报信息
     *
     * @param adSpScDetailPoster 海报广告信息，包含（标识，海报地址，分辨率，广告分屏明细id）
     * @return 成功条数
     */
    @Override
    public Integer updateAdSpScDetailPoster(AdSpScDetailPoster adSpScDetailPoster) {
        Connection conn = null;
        Statement statement = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "update `ad_sp_sc_detail_poster` set";

            //3.2判断条件并拼接
            if (adSpScDetailPoster.getPosterUrl() != null && adSpScDetailPoster.getPosterUrl().trim().length() > 0) {
                sql += " poster_url='" + adSpScDetailPoster.getPosterUrl() + "'";
            }
            if (adSpScDetailPoster.getResolution() != null) {
                sql += " , resolution=" + adSpScDetailPoster.getResolution() + "";
            }
            sql += " where id=" + adSpScDetailPoster.getId();

            //4.执行sql
            int count = statement.executeUpdate(sql);

            //5.返回成功条数
            return count;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement);
        }
    }

    /**
     * 删除海报信息
     *
     * @param id 海报标识
     * @return 成功条数
     */
    @Override
    public Integer deleteAdSpScDetailPoster(Long id) {
        Connection conn = null;
        Statement statement = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "delete from `ad_sp_sc_detail_poster` where id=" + id;

            //4.执行sql
            int count = statement.executeUpdate(sql);

            //5.返回成功条数
            return count;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement);
        }
    }

    /**
     * 查找海报信息
     *
     * @param id 海报标识
     * @return 查找到的信息（包含标识，海报地址，分辨率，广告分屏明细id）
     */
    @Override
    public AdSpScDetailPoster getAdSpScDetailPosterById(Long id) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "select `id`,`poster_url`,`resolution`,`ad_sp_sc_detail_id`  from `ad_sp_sc_detail_poster` where id=" + id;

            //4.执行sql获取结果集
            rs = statement.executeQuery(sql);

            //只有一条数据通过if判断，将指针跳到数据行
            if (rs.next()) {
                AdSpScDetailPoster adSpScDetailPoster = new AdSpScDetailPoster();
                adSpScDetailPoster.setId(rs.getLong("id"));
                adSpScDetailPoster.setPosterUrl(rs.getString("poster_url"));
                adSpScDetailPoster.setResolution(rs.getLong(("resolution")));
                adSpScDetailPoster.setAdSpScDetailId(rs.getLong("ad_sp_sc_detail_id"));

                //5.返回搜索到的对象
                return adSpScDetailPoster;
            }

            return null;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement, rs);
        }
    }

    /**
     * 查询海报信息
     *
     * @param adSpScDetailPoster 查询条件 包含标识，海报地址，分辨率，广告分屏明细id
     * @return 查询到的信息列表（包含标识，海报地址，分辨率，广告分屏明细id）
     */
    @Override
    public List<AdSpScDetailPoster> searchAdSpScDetailPoster(AdSpScDetailPoster adSpScDetailPoster) {
        Connection conn = null;
        Statement statement = null;
        ResultSet rs = null;
        List<AdSpScDetailPoster> list = new ArrayList<AdSpScDetailPoster>();
        //1.连接数据库
        try {
            //1.连接数据库
            conn = JdbcUtils.getConnection();

            //2.获取数据库执行器
            statement = conn.createStatement();

            //3.定义sql语句
            String sql = "select `id`,`poster_url`,`resolution`,`ad_sp_sc_detail_id`  from `ad_sp_sc_detail_poster` where 1=1";

            //3.2根据条件拼接
            if (adSpScDetailPoster.getAdSpScDetailId() != null) {
                sql += " and ad_sp_sc_detail_id=" + adSpScDetailPoster.getAdSpScDetailId();
            }

            //4.执行sql获取结果集
            rs = statement.executeQuery(sql);

            while (rs.next()) {
                //将结果集数据存储到adSpScDetailPoster中
                AdSpScDetailPoster adSpScDetailPoster1 = new AdSpScDetailPoster();
                adSpScDetailPoster1.setId(rs.getLong("id"));
                adSpScDetailPoster1.setPosterUrl(rs.getString("poster_url"));
                adSpScDetailPoster1.setResolution(rs.getLong(("resolution")));
                adSpScDetailPoster1.setAdSpScDetailId(rs.getLong("ad_sp_sc_detail_id"));

                //将对象插入到数组中
                list.add(adSpScDetailPoster1);
            }


            return list;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            //关闭数据库
            JdbcUtils.close(conn, statement, rs);
        }
    }
}
